#define  _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

size_t My_strlen1(const char* dest)
{
	size_t count = 0;
	while (*dest)
	{
		count++;
		dest++;
	}

	return count;
}
size_t My_strlen2(const char* dest)
{
	const char* tail = dest;
	while (*tail++)
	{
		;
	}
	tail--;

	return tail - dest;
}
size_t My_strlen3(const char* dest)
{
	if (*dest == '\0')
	{
		return 0;
	}
	else
	{
		return 1 + My_strlen3(dest + 1);
	}
}
char* My_strcpy(char* dest, const char* src)
{
	assert(dest && src);
	char* ret = dest;

	while (*dest++ = *src++)
	{
		;
	}

	return ret;
}
char* My_strcat(char* dest, const char* src)
{
	assert(dest && src);
	char* ret = dest;

	while (*dest)
	{
		dest++;
	}

	while (*dest++ = *src++)
	{
		;
	}

	return ret;
}
int My_strcmp(const char* s1, const char* s2)
{
	assert(s1 && s2);

	while (*s1 == *s2)
	{
		if (*s1 == '\0')
		{
			return 0;
		}

		s1++;
		s2++;
	}
	if (*s1 > *s2)
		return 1;
	else
		return - 1;
}
char* My_strstr1(const char* str1, const char* str2)
{
	assert(str1 && str2);
	const char* s1 = str1;
	const char* s2 = str2;
	const char* cur = str1;

	while (*cur)
	{
		s1 = cur;
		s2 = str2;
		while (*s1 && *s2 && (*s1 == *s2))
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')
		{
			return (char*)cur;
		}

		cur++;
	}

	return NULL;
}
char* My_strstr(const char* str1, const char* str2)
{
	assert(str1 && str2);
	const char* s1 = str1;
	const char* s2 = str2;
	const char* cur = str1;

	while (*cur)
	{
		s1 = cur;
		s2 = str2;
		while (*s1 && *s2 && (*s1 == *s2))
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')
		{
			return (char*)cur;
		}

		cur++;
	}

	return NULL;
}

void* My_memcpy(void* dest, const void* src, size_t sz)
{
	void* ret = dest;
	while (sz--)
	{
		*(char*)dest = *(char*)src;
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}

	return ret;
}
void* My_memmove(void* dest, const void* src, size_t sz)
{
	void* ret = dest;
	if (dest < src)
	{
		while (sz--)
		{
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else
	{
		while (sz--)
		{
			*((char*)dest + sz) = *((char*)src + sz);
		}
	}
}

void* My_strncat(char* dest, const char* src, size_t num)
{
	assert(dest && src);

	char* tail = dest;
	while (*tail++)
	{
		;
	}
	tail--;

	while (num--)
	{
		*tail++ = *src++;
	}

	*tail = '\0';
}
void* My_strncpy(char* dest, const char* src, size_t num)
{
	assert(dest && src);

	while (num--)
	{
		*dest++ = *src++;
	}

	*dest = '\0';
}
int main()
{
	char ch[20] = "acdabcdef";
	char ch1[20] = { 0 };
	char ch2[] = "xxxxx";
	char ch3[] = "abc";
	//My_strncat(ch, ch2, 3);
	My_strncpy(ch, ch2, 3);
	printf("%s\n",ch);

	//printf("%u\n", My_strlen1(ch));
	//printf("%u\n", My_strlen2(ch));
	//printf("%u\n", My_strlen3(ch));
	//printf("%s\n", My_strcpy(ch1, ch));
	//printf("%d\n", My_strcmp(ch1, ch));
	//printf("%s\n", My_strcat(ch1, ch2));
	//printf("%s\n",My_strstr1(ch, ch3));

	//int n = 10;

	//for (int i = 2; i < 10; i++)
	//{
	//	if (n%i == 1)
	//	{
	//		printf("%d\n",n);
	//		break;
	//	}
	//}

	//char str[] = "agfs@afa.afafdsa";
	//char buf[30] = { 0 };
	//const char* tep = "@.";

	//strcpy(buf, str);

	//char* str1 = NULL;
	//for (str1 = strtok(buf, tep); str1 != NULL; str1 = strtok(NULL, tep))
	//{
	//	printf("%s\n",str1);
	//}

	//int arr[10] = { 0 };
	//int arr[] = { 0 };
	//memset(arr, 1, 20);
	//memcmp(arr, arr1);

	return 0;
}